<template>
  <div class="my-container">
    <div class="header not-login" v-if="!user">
      <div class="login-btn" @click="$router.push('/login')">
        <img src="~@/assets/mobile.png" alt />
        <span>登录 / 注册</span>
      </div>
    </div>
    <div class="user-info" v-else>
      <div class="base-info">
        <div class="left">
          <van-image round fit="cover" class="head-img" :src="userInfo.photo" />
          <span class="name">{{userInfo.name}}</span>
        </div>
        <div class="right">
          <van-button class="edit-btn" size="mini" round type="default">编辑资料</van-button>
        </div>
      </div>
      <div class="data-states">
        <div class="data-item">
          <span class="count">{{userInfo.art_count}}</span>
          <span class="text">头条</span>
        </div>
        <div class="data-item">
          <span class="count">{{userInfo.follow_count}}</span>
          <span class="text">关注</span>
        </div>
        <div class="data-item">
          <span class="count">{{userInfo.fans_count}}</span>
          <span class="text">粉丝</span>
        </div>
        <div class="data-item">
          <span class="count">{{userInfo.like_count}}</span>
          <span class="text">获赞</span>
        </div>
      </div>
    </div>
    <!-- grid区域 -->
    <van-grid class="grid-nav" :column-num="2" clickable>
      <van-grid-item class="grid-item">
        <i slot="icon" class="iconfont iconshoucang"></i>
        <span slot="text" class="text">收 藏</span>
      </van-grid-item>
      <van-grid-item class="grid-item">
        <i slot="icon" class="iconfont iconlishi"></i>
        <span slot="text" class="text">历 史</span>
      </van-grid-item>
    </van-grid>
    <!-- 消息通知和小智同学区域 -->
    <van-cell is-link @click="showPopup">消息通知</van-cell>
    <van-cell is-link @click="showPopup">小智同学</van-cell>
    <van-cell clickable v-if="user" class="log-out-cell" @click="showPopup">退出登录</van-cell>
    <!-- <van-popup v-model="show">内容</van-popup> -->

    <!-- 退出登录区域 -->
  </div>
</template>

<script>
import { getUserInfo } from '@/api/user'
import { mapState } from 'vuex'
export default {
  data() {
    return {
      userInfo: {} // 用户信息
    }
  },
  computed: {
    ...mapState(['user'])
  },
  created() {
    if (this.user) {
      this.loadUser()
    }
  },
  methods: {
    showPopup() {
      //   this.show = true
      this.$dialog
        .confirm({
          title: '标题',
          message: '弹窗内容'
        })
        .then(() => {
          // on confirm
          console.log('确认')
          this.$store.commit('setUser', null)
        })
        .catch(() => {
          // on cancel
          console.log('取消')
        })
    },
    async loadUser() {
      try {
        const { data: res } = await getUserInfo()
        console.log(res)
        this.userInfo = res.data
      } catch (err) {
        // console.log(err)
        this.$toast('获取数据失败')
      }
    }
  }
}
</script>

<style scoped lang='less'>
.my-container {
  .header {
    height: 361px;
    //   background-color: red;
    background: url('~@/assets/banner.png') no-repeat;
    background-size: cover;
    display: flex;
    justify-content: center;
    align-items: center;
    .login-btn {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      img {
        width: 132px;
        height: 132px;
      }
      span {
        font-size: 28px;
        margin-top: 16px;
        color: #fff;
      }
    }
  }
  .user-info {
    background: url('~@/assets/banner.png') no-repeat;
    background-size: cover;
    .base-info {
      height: 231px;
      //   background-color: pink;
      padding: 76px 32px 23px;
      box-sizing: border-box;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .left {
        display: flex;
        align-items: center;
        .head-img {
          width: 132px;
          height: 132px;
          border: 5px solid #fff;
          margin-right: 23px;
        }
        .name {
          font-size: 30px;
          color: #fff;
        }
      }
      .right {
        display: flex;
        align-items: center;
        .edit-btn {
          //   width: 115px;
          height: 32px;
          font-size: 20px;
          //   color: #ccc;
        }
      }
    }
    .data-states {
      //   height: 130px;
      //   background-color: #ccc;
      display: flex;

      .data-item {
        height: 130px;
        flex: 1;
        display: flex;
        justify-content: center;
        align-items: center;
        flex-direction: column;
        color: #fff;
        .count {
          font-size: 36px;
        }
        .text {
          font-size: 23px;
        }
      }
    }
  }
  .grid-nav {
    margin-bottom: 5px;
    .grid-item {
      height: 141px;
      i.iconfont {
        font-size: 45px;
      }
      .iconshoucang {
        color: #eb5253;
      }
      .iconlishi {
        color: #ff9d1d;
      }

      span.text {
        font-size: 28px;
      }
    }
  }
  .log-out-cell {
    margin-top: 5px;
    .van-cell__value--alone {
      text-align: center;
      color: red;
    }
  }
}
</style>
